package dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.ConnectionPoolDataSource;
import java.sql.SQLException;
import java.util.List;


public class BaseDao<T> {
    QueryRunner runner = new QueryRunner(new ComboPooledDataSource());

    public int myupdate(String sql, Object... parms) {
        try {
            return runner.update(sql, parms);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public T myselect_one(String sql, Class<T> type, Object... parms) {
        try {
            return runner.query(sql, new BeanHandler<T>(type), parms);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<T> myselesct_multi(String sql, Class<T> type, Object... parms) {
        try {
            return runner.query(sql, new BeanListHandler<T>(type), parms);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public  Object myscale(String sql, Object... params) {
        try {
            return runner.query(sql, new ScalarHandler<>(), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
